package br.com.emanuelvictor.architectureandroid.app1.fragment.picker;

import java.util.Calendar;
import java.util.List;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.TimePicker;
import br.com.emanuelvictor.architectureandroid.app1.entity.Daily;
import br.com.emanuelvictor.architectureandroid.app1.entity.Point;
import br.com.emanuelvictor.architectureandroid.app1.repository.PointRepository;
import br.com.emanuelvictor.architectureandroid.app1.support.DatabaseHelper;
import br.com.emanuelvictor.architectureandroid.app1.util.PointVariables;
import br.com.emanuelvictor.architectureandroid.util.Interface;
import br.com.emanuelvictor.architectureandroid.util.Navigation;

public class TimePickerFragment extends DialogFragment implements
		OnTimeSetListener {

	private boolean cancel = false;
	private Calendar calendar;
	private int minute;
	private int hourOfDay;
	private TimePickerDialog timePickerDialog;
	private int index;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		this.index = getArguments().getInt("index", 100);
		this.calendar = ((PointVariables) ((Navigation) getActivity())
				.getVariables()).getHistoryNavigation().getCurrent();

		PointRepository repository = new PointRepository(new DatabaseHelper(
				getActivity()));
		Daily dailyHandler = new Daily(repository, calendar);

		List<Point> points = dailyHandler.getPoints();

		Boolean isEditing = getArguments().getBoolean("isEditing", false);

		if (isEditing) {
			calendar = points.get(index).date;
		}

		this.hourOfDay = this.calendar.get(Calendar.HOUR_OF_DAY);
		this.minute = this.calendar.get(Calendar.MINUTE);

		this.timePickerDialog = new TimePickerDialog(getActivity(), this,
				hourOfDay, minute, true);

		return this.timePickerDialog;

	}

	@Override
	public void onCancel(DialogInterface dialog) {
		this.cancel = true;
		super.onCancel(dialog);
	}

	@Override
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		this.hourOfDay = hourOfDay;
		this.minute = minute;

	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		if (!cancel) {
			try {
				PointRepository repository = new PointRepository(
						new DatabaseHelper(getActivity()));
				Daily dailyHandler = new Daily(repository, calendar);

				List<Point> points = dailyHandler.getPoints();

				Boolean isEditing = getArguments().getBoolean("isEditing",
						false);

				this.calendar.set(Calendar.MINUTE, minute);
				this.calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);

				Point p = new Point();
				if (isEditing) {
					p.id = points.get(index).id;
				}
				p.date = this.calendar;
				this.insert(
						p,
						new PointRepository(new DatabaseHelper(this
								.getActivity())), isEditing);
			} catch (Exception e) {

			}
		}
		super.onDismiss(dialog);
	}

	public void insert(Point p, PointRepository repository, boolean isEditing)
			throws Exception {

		if (isEditing) {
			repository.update(p);
		} else {
			repository.insert(p);
		}
		((Interface) this.getActivity()).update();

	}

}
